package com.iwe.crm.workbench.service.impl.impl;

import com.iwe.crm.base.util.CommonUtil;
import com.iwe.crm.base.util.DateUtil;
import com.iwe.crm.user.bean.User;
import com.iwe.crm.workbench.bean.Customer;
import com.iwe.crm.workbench.bean.StageVo;
import com.iwe.crm.workbench.bean.Tran;
import com.iwe.crm.workbench.bean.TranHistory;
import com.iwe.crm.workbench.mapper.CustomerMapper;
import com.iwe.crm.workbench.mapper.TranHistoryMapper;
import com.iwe.crm.workbench.mapper.TranMapper;
import com.iwe.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class TranServiceImpl implements TranService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private TranMapper tranMapper;

    @Autowired
    private TranHistoryMapper tranHistoryMapper;

    @Override
    public List<String> queryCustomerName(String customerName) {
        Example example = new Example(Customer.class);
        example.createCriteria().andLike("name","%"+customerName+"%");
        List<Customer> customers = customerMapper.selectByExample(example);
        //定义泛型是String类型的集合，存储客户名称
        List<String> names = new ArrayList<>();
        for (Customer customer : customers) {
            names.add(customer.getName());
        }

        return names;
    }

    @Override
    public Map<String,Object> queryStages(String id, Map<String,String> stage2PossibilityMap
    , Integer index1, User user) {

        //定义一个Map给客户端返回数据
        Map<String,Object> map = new HashMap<>();
        //先获取交易的对象
        Tran tran = tranMapper.selectByPrimaryKey(id);
        //获取交易阶段
        String currentStage = tran.getStage();
        //获取交易可能性i
        String currentPossibility = tran.getPossibility();
        //把阶段和可能性的Map数据转换成List,希望获取索引值
        List<Map.Entry<String,String>> list = new ArrayList(stage2PossibilityMap.entrySet());
        if(index1 != null){
            //点击阶段图标
            currentStage = list.get(index1).getKey();
            currentPossibility = list.get(index1).getValue();
            tran.setStage(currentStage);
            tran.setPossibility(currentPossibility);
            //更新数据库
            tranMapper.updateByPrimaryKeySelective(tran);

            //添加一条交易历史
            TranHistory tranHistory = new TranHistory();
            tranHistory.setId(CommonUtil.uuid());
            tranHistory.setStage(currentStage);
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setCreateTime(DateUtil.dateToString(new Date()));
            tranHistory.setCreateBy(user.getName());
            tranHistory.setTranId(tran.getId());
            tranHistory.setPossibility(currentPossibility);
            tranHistoryMapper.insertSelective(tranHistory);


            //放入交易历史
            map.put("tranHistory",tranHistory);
        }

        //定义一个存储给前台返回的交易阶段图标集合
        List<StageVo> stages = new ArrayList<>();
        //获取所有交易阶段中第一个可能性为0的阶段
        int position = 0;
        for(int i = 0; i < list.size(); i++){
            Map.Entry<String, String> entry = list.get(i);
            String possibility = entry.getValue();
            if(possibility.equals("0")){
                position = i;
                break;
            }
        }
        //获取当前交易所处阶段在所有阶段中的位置
        int index = 0;
        for(int i = 0; i < list.size(); i++){
            Map.Entry<String, String> entry = list.get(i);
            String possibility = entry.getValue();
            if(possibility.equals(currentPossibility)){
                index = i;
                break;
            }
        }
        if(currentPossibility.equals("0")){
            //交易失败了  判断左边7个黑圈，右边2个X
            for(int i = 0; i < list.size(); i++){
                StageVo stageVo = new StageVo();
                Map.Entry<String, String> entry = list.get(i);
                //每一个阶段
                String stage = entry.getKey();
                //阶段对应的可能性
                String possibility = entry.getValue();

                if(possibility.equals("0")){
                    if(currentStage.equals(stage)){
                        stageVo.setType("红x");
                    }else{
                        stageVo.setType("黑x");
                    }
                }else{
                    stageVo.setType("黑圈");
                }
                stageVo.setStage(stage);
                stageVo.setContent(stage + ":" + possibility);

                stages.add(stageVo);
            }

        }else{
            //交易中
            for(int i = 0; i < list.size(); i++){
                StageVo stageVo = new StageVo();
                Map.Entry<String, String> entry = list.get(i);
                String stage = entry.getKey();
                String possibility = entry.getValue();
                if(i < index){
                    stageVo.setType("绿圈");
                }else if(i == index){
                    stageVo.setType("锚点");
                }else if(i > index && i < position){
                    stageVo.setType("黑圈");
                }else{
                    stageVo.setType("黑x");
                }

                stageVo.setStage(stage);
                stageVo.setContent(stage + ":" + possibility);
                stages.add(stageVo);
            }

        }
        //添加交易阶段图标
        map.put("stages",stages);
        //添加阶段和可能性
        map.put("stage",currentStage);
        map.put("possibility",currentPossibility);

        return map;
    }
}
